package com.fh.modules.pms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.common.util.StringUtils;
import com.fh.modules.pms.ProductConstant;
import com.fh.modules.pms.entity.PmsProduct;
import com.fh.modules.pms.model.ProductAttributeModel;
import com.fh.modules.pms.service.IPmsProductPropertyService;
import com.fh.modules.pms.service.IPmsProductService;
import com.fhMall.request.QueryProductListRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Title: Controller
 * @Description: 商品
 * @author： code generator
 * @date： 2019-09-11
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("商品管理")
public class PmsProductController {

    @Autowired
    private IPmsProductService pmsProductService;
    @Autowired
    private IPmsProductPropertyService pmsProductPropertyService;

    @GetMapping(value = "/pms/pmsProduct/list")
    @ApiOperation("分页列表查询")
    @AutoLog(value="商品库存分页查询",logType=CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(PmsProduct pmsProduct,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<PmsProduct>();
        pmsProduct.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, pmsProduct);
        Page<PmsProduct> page = new Page<PmsProduct>(pageNo, pageSize);
        IPage<PmsProduct> pageList = pmsProductService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     *  移动端分页查询，不会返回总数
     * @param queryProductListRequest
     * @return
     */
    @GetMapping(value = "/pms/pmsProduct/queryPageList",consumes = "application/json")
    @ApiOperation("移动端分页查询")
    @AutoLog(value = "移动端分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageList(@RequestBody QueryProductListRequest queryProductListRequest,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        PmsProduct pmsProduct = new PmsProduct();
        BeanUtils.copyProperties(pmsProduct, queryProductListRequest);
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<PmsProduct>();
        pmsProduct.setDelFlag(CommonConstant.DEL_FLAG_0);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, pmsProduct);
        Page<PmsProduct> page = new Page<PmsProduct>(pageNo, pageSize);
        IPage<PmsProduct> pageList = pmsProductService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @GetMapping(value = "/pms/pmsProduct/queryProductByCategory",consumes = "application/json")
    @ApiOperation("移动端分页查询")
    @AutoLog(value = "移动端分页查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryProductByCategory(Integer categoryId,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        PmsProduct pmsProduct = new PmsProduct();
        QueryWrapper<PmsProduct> queryWrapper = new QueryWrapper<PmsProduct>();
        pmsProduct.setDelFlag(CommonConstant.DEL_FLAG_0);
        pmsProduct.setProductCategoryId(categoryId);
        pmsProduct.setPublishStatus(1);
        QueryWrapperUtil.initQueryWrapper(queryWrapper, pmsProduct);
        Page<PmsProduct> page = new Page<PmsProduct>(pageNo, pageSize);
        IPage<PmsProduct> pageList = pmsProductService.page(page, queryWrapper);
        return Result.ok(pageList);
    }




    @GetMapping(value = "/pms/pmsProduct/queryProductProperty")
    @ApiOperation("查询商品的属性列表")
    @AutoLog(value = "查询商品的属性列表", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryProductProperty(Integer id){
        PmsProduct pmsProduct = pmsProductService.getById(id);
        //用map封装一下，方便以后增加返回结果集，这样不会破坏原有返回结果数据结构
        ProductAttributeModel productAttributeModel=new ProductAttributeModel();
        productAttributeModel.setProperty(pmsProductPropertyService.queryProperty(pmsProduct.getProductAttributeGroupId()));
        return Result.ok(productAttributeModel);
    }


    @GetMapping(value = "/pms/pmsProduct/queryPageByKeyword")
    @ApiOperation("根据关键字搜索商品")
    @AutoLog(value = "根据关键字搜索商品", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryPageByKeyword(String keyword,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return Result.ok(pmsProductService.queryProductPage(keyword,pageNo,pageSize));
    }


    @PostMapping(value = "/pms/pmsProduct/onPublish")
    @ApiOperation("商品上架")
    @AutoLog("商品上架")
    public Result onPublish(@RequestBody  List<Integer> ids){
        ids.forEach(id->{
            pmsProductService.onOffProduct(id, ProductConstant.ON_PRODUCT);
        });
        return Result.ok("上架成功");
    }


    @PostMapping(value = "/pms/pmsProduct/offPublish")
    @ApiOperation("商品下架")
    @AutoLog("商品下架")
    public Result offPublish(@RequestBody List<Integer> ids){
        ids.forEach(id->{
            pmsProductService.onOffProduct(id, ProductConstant.OFF_PRODUCT);
        });
        return Result.ok("下架成功");
    }

    @PostMapping(value = "/pms/pmsProduct/add")
    @ApiOperation("添加")
    @AutoLog("添加商品")
    public Result add(@RequestBody PmsProduct pmsProduct) {
        pmsProduct.setPublishStatus(0);
        pmsProduct.setBuyStepNum(1);
        pmsProductService.save(pmsProduct);
        return Result.ok("添加成功");
    }

    @PutMapping(value = "/pms/pmsProduct/edit")
    @ApiOperation("修改")
    @AutoLog("修改商品")
    public Result edit(@RequestBody PmsProduct pmsProduct) {
        boolean ok = pmsProductService.updateById(pmsProduct);
        return ok ? Result.ok("修改成功") : Result.error("修改失败");
    }

    @DeleteMapping(value = "/pms/pmsProduct/deleteBatch")
    @ApiOperation("批量删除")
    @AutoLog("批量删除商品")
    public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        pmsProductService.deleteBatchEntityByDelFlag(ids);
        return Result.ok("批量删除成功");
    }

    @DeleteMapping(value = "/pms/pmsProduct/delete")
    @ApiOperation("通过id删除")
    @AutoLog("删除商品")
    public Result delete(@RequestParam(name="id",required=true) String id) {
        pmsProductService.deleteEntityByDelFlag(id);
        return Result.ok("删除成功");
    }

    @GetMapping(value = "/pms/pmsProduct/queryById")
    @ApiOperation("通过id查询")
    @AutoLog(value = "商品通过id查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryById(@RequestParam(name = "id", required = true) String id) {
        PmsProduct pmsProduct = pmsProductService.getById(id);
        return Result.ok(pmsProduct);
    }

    @GetMapping(value = "/pms/pmsProduct/queryByIds")
    @ApiOperation("通过ids查询")
    @AutoLog(value = "商品通过ids查询", logType = CommonConstant.LOG_TYPE_QUERY)
    public Result queryByIds(@RequestParam(name = "ids", required = true) String ids) {
        String[] idArray=ids.split(",");
        List<PmsProduct> products=new ArrayList<>();
        for (String id : idArray) {
            if(!StringUtils.isEmpty(id)){
                PmsProduct pmsProduct = pmsProductService.getById(id);
                products.add(pmsProduct);
            }
        }
        return Result.ok(products);
    }

}
